package com.augurit.gzcgwsp.smartformCsdlwj.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.dozer.DozerBeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.augurit.gzcgwsp.smartformCsdlwj.dao.YwHwXmryDao;
import com.augurit.gzcgwsp.smartformCsdlwj.entity.YwHwXmry;
import com.augurit.gzcgwsp.smartformCsdlwj.service.IYwHwXmryService;
import com.augurit.gzcgwsp.smartformCsdlwj.web.form.YwHwXmryForm;

/*
 * 陆域生活垃圾清扫车辆情况service实现类
 */
@Service
@Transactional
public class YwHwXmryServiceImpl  implements IYwHwXmryService{
	@Autowired
	private YwHwXmryDao ywHwXmryDao;
	
	/**
	 * 获取form
	 */
	public YwHwXmryForm getForm(Long Id) {
		YwHwXmry entity=ywHwXmryDao.get(Id);
		YwHwXmryForm form=new DozerBeanMapper().map(entity,YwHwXmryForm.class);
		return form;
	}

	public List<YwHwXmryForm> getListByXmId(Long xmid) {
		List<YwHwXmry> list=this.getListByXmid(xmid);
		List<YwHwXmryForm> forms=new ArrayList<YwHwXmryForm>();
		for(YwHwXmry entity:list){
			YwHwXmryForm form=new DozerBeanMapper().map(entity,YwHwXmryForm.class);
			forms.add(form);
		}
		return forms;
	}

	/**
	 * 根据qyid查询数据
	 * @param QYId
	 * @return
	 */
	private List<YwHwXmry> getListByXmid(Long xmid){
		String sql=" from "+YwHwXmry.class.getName()+" where  xmid="+xmid;
		List<YwHwXmry> list=this.ywHwXmryDao.find(sql);
		return list;
	}


	public void SaveOrUpdate(YwHwXmryForm form) {
		YwHwXmry entity;
		if(form != null && form.getId() != null){
			entity =ywHwXmryDao.get(form.getId());
		}else{
			entity = new YwHwXmry();
		}
		new DozerBeanMapper().map(form,entity);
		this.ywHwXmryDao.save(entity);
	}

	public void SaveOrUpdate(YwHwXmry entity) {
		this.ywHwXmryDao.save(entity);
	}

	public void parseYwHwXmry(HSSFSheet hssfsheet,Long xmid){
		try {
			int startrow =0;
			HSSFRow hssfrow = hssfsheet.getRow(0);// 第一行
			for (int j = 1; j < hssfsheet.getPhysicalNumberOfRows(); j++) {
				hssfrow = hssfsheet.getRow(j);
				if(hssfrow != null){
					String value = getCellValue(hssfrow, 0);
					if(value.equals("项目情况登记表（三）")){
						startrow = j;
					}
				}
			}
			if(startrow>0 ){
				for(int i =startrow+2;i<hssfsheet.getPhysicalNumberOfRows();i++){
					hssfrow = hssfsheet.getRow(i);
					if(hssfrow != null){
						String ygxm = getCellValue(hssfrow, 1);
						String xb = getCellValue(hssfrow, 2);
						String csny = getCellValue(hssfrow, 3);
						String zzmm = getCellValue(hssfrow, 4);
						String sfzh = getCellValue(hssfrow, 5);
						String csgw = getCellValue(hssfrow, 6);
						String cysj = getCellValue(hssfrow, 7);
						if(ygxm!=null && ygxm.length()>0){
							YwHwXmry ywHwXmry = new YwHwXmry();
							ywHwXmry.setCsgw(csgw);
							ywHwXmry.setCsny(csny);
							ywHwXmry.setCysj(cysj);
							ywHwXmry.setSfzh(sfzh);
							ywHwXmry.setXb(xb);
							ywHwXmry.setXmid(xmid);
							ywHwXmry.setYgxm(ygxm);
							ywHwXmry.setZzmm(zzmm);
							this.SaveOrUpdate(ywHwXmry);
						}
					}
				}
			}
						
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private String getCellValue(HSSFRow hssfrow,int col){
		String value;
		if (hssfrow.getCell((short) col) == null) {
			value = "";
		} else if (hssfrow.getCell((short) col).getCellType() == 0) {
			value = new Double(hssfrow.getCell((short) col).getNumericCellValue()).toString();
		} else {// 如果EXCEL表格中的数据类型为字符串型
			value = hssfrow.getCell((short) col).getStringCellValue().trim();
		}
		return value;
	}
}
